/**
 *
 */
package com.vrv.framework.common.utils;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import jp.ne.so_net.ga2.no_ji.jcom.IDispatch;
import jp.ne.so_net.ga2.no_ji.jcom.JComException;
import jp.ne.so_net.ga2.no_ji.jcom.ReleaseManager;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.vrv.common.config.Configuration;
import com.vrv.common.utils.StringUtil;
import com.vrv.netmgr.listener.ApplicationListener;

/**
 * 单例加解密工具类
 *
 * @author 曹保利
 *
 */
@SuppressWarnings("unchecked")
public class EdpCryptHelper {

    private static Logger logger = LoggerFactory.getLogger(EdpCryptHelper.class);

    public static final String COM_NAME = "EdpCrypt.TextCrypt";
    public static final String ENCODE_METHOD = "textEncode";
    public static final String DECODE_METHOD = "textDecode";

    /** 系统解密需要的密钥 **/
    private static Long encryptTableKey = 0L;
    /** 内存存放加密表以及该表的加密字段 **/
    private static Map<String, String> encryptTableMetalMap = new HashMap<String, String>();
    /** 单例实例构造初始化加密相关参数 **/
    private static final EdpCryptHelper cryptor = new EdpCryptHelper();

    /**
     * 构造初始化相关数据
     */
    private EdpCryptHelper() {
        encryptTableKey = (Long) ApplicationListener.getContext().getAttribute("encryptTableKey");
        encryptTableMetalMap = (Map<String, String>) ApplicationListener.getContext().getAttribute("encryptTableMetalMap");
        logger.debug(getClass().getSimpleName() + "		{encryptTableKey:" + encryptTableKey + "} ");
        logger.debug(getClass().getSimpleName() + "		{encryptTableMetalMap:" + encryptTableMetalMap.size() + "} ");
    }

    /**
     * 使用单例模式，返回配置类实例.
     *
     * @return Configuration
     */
    public static EdpCryptHelper getInstance() {
        return cryptor;
    }

    /**
     * 解密
     */
    public String cryptText(String method, String text) {
        try {
            ReleaseManager rm = new ReleaseManager();
            IDispatch com = new IDispatch(rm, COM_NAME);
            String val = (String) com.method(method, new Object[] { text, String.valueOf(encryptTableKey), false });
            return val;
        } catch (JComException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 判断是否在加密配置中
     *
     * @param tableName
     * @param columnName
     */
    public boolean detecte(String tableName, String columnName) {
        if (encryptTableMetalMap.containsKey(tableName)) {
            String values = encryptTableMetalMap.get(tableName);
            String[] columns = values.toUpperCase().replace("_", "").split(",");
            Arrays.sort(columns);
            String searchKey = columnName.replace("_", "").toUpperCase();
            int index = Arrays.binarySearch(columns, searchKey);
            if (index > -1) {
                return Boolean.TRUE;
            }
            // values = values.toUpperCase().replace("_", "");
            // if (values.contains(columnName.toUpperCase().replace("_", ""))) {
            // return Boolean.TRUE;
            // }
        }
        return false;
    }

    public String getConfigCryptTable(String className, String property) {
        String key = className + "#" + property;
        String tableName = Configuration.getInstance().getValue(key);
        if (StringUtil.isValidString(tableName)) {
            return tableName;
        } else {
            tableName = Configuration.getInstance().getValue(className);
        }
        return tableName;
    }

}
